package demo.virtuemart.Util;



public class TestUtil{


	public static boolean isSuiteRunnable(xls_Reader xlsreader, String suiteName) throws Throwable
	{
		boolean isExecutable = false;
	//	int rowcount = xlsreader.getRowCount("Test Suite");
		for(int i=1;i<xlsreader.getRowCount("Test Suite"); i++)
		{
			String suite= xlsreader.getCellData("Test Suite", "TSID", i);
			
			String runmode = xlsreader.getCellData("Test Suite", "Runmode", i);
			System.out.println(suite+" ------" +runmode);
			if(suite.equals(suiteName))
			{
				if(runmode.equalsIgnoreCase("Y"))
				{
					isExecutable = true;
					
				}
				else if(runmode.equalsIgnoreCase("N"))
				{
					isExecutable = false;
				}
			}
			
		}
		xlsreader=null;
		return isExecutable;
	}
	
	public static boolean isTestCaseRunnable(xls_Reader xlsreader, String testCaseName) throws Throwable
	{
		boolean isExecutabletestcase = false;
		//int rowcount = xlsreader.getRowCount("Test Cases");
		for(int i=1;i<xlsreader.getRowCount("Test Cases"); i++)
		{
			String testCase= xlsreader.getCellData("Test Cases", "TCID", i);
			
			String runmode = xlsreader.getCellData("Test Cases", "Runmode", i);
			System.out.println(testCase+ "-------" +runmode);
			if(testCase.equals(testCaseName))
			{
				if(runmode.equalsIgnoreCase("Y"))
				{
					isExecutabletestcase = true;
					//System.out.println(testCase+" ------" +runmode);
				}
				else
				{
					isExecutabletestcase = false;
				}
			}
			
		}
		xlsreader=null;
		return isExecutabletestcase;
	}
	
	public static Object[][] getData(xls_Reader xlsreader, String testCaseName) throws Throwable
	{
		if(!xlsreader.isSheetExist(testCaseName))
		{
			xlsreader=null;
			return new Object[1][0];
		}
		int row = xlsreader.getRowCount(testCaseName);
		int col = xlsreader.getColCount(testCaseName);
		Object[][] data = new Object[row-1][col-2];
		for(int rowNum=1;rowNum<row;rowNum++)
		{
			for(int colNum=0;colNum<col-2;colNum++)
			{
		     data[rowNum-1][colNum]=xlsreader.getCellData(testCaseName, colNum, rowNum);
		     System.out.print(data[rowNum-1][colNum]+ " ");
			}
			System.out.println();
		}
		return data;
	}
	public static void reportDataSetResult(xls_Reader xls, String testCaseName, int rowNum,String result) throws Throwable
	{
		
		     xls.setCellData(testCaseName, "Results", rowNum, result);
		
	}
	public static String[] getDataSetRunModes(xls_Reader xls, String sheetName) throws Throwable
			{
		String[] runmode = null;
		      if(!xls.isSheetExist(sheetName))
		      {
		    	  xls = null;
		    	  sheetName=null;
		    	  runmode = new String[1];
		    	  runmode[0]="Y";
		    	  xls = null;
		    	  sheetName=null;
		    	  return runmode;
		    	  
		      }
		      runmode = new String[xls.getRowCount(sheetName)-1];
		      for(int i=1;i<=runmode.length;i++)
		      {
		    	  runmode[i-1]= xls.getCellData(sheetName, "Runmode", i);
		      }
		      xls=null;
		      sheetName=null;
		      return runmode;
			}
	
	// return the row num for a test
			public static int getRowNum(xls_Reader xls, String testCaseName) throws Throwable{
				int rowCount = xls.getRowCount("Test Cases");
				for(int i=1; i<= xls.getRowCount("Test Cases"); i++){
					String tcid=xls.getCellData("Test Cases", "TCID", i);
					
					if(tcid.equals(testCaseName)){
						xls=null;
						return i;
					}
					
					
				}
				
				return -1;
			}
}
